package cn.blog.controller;

import cn.blog.entity.Blogtype;
import cn.blog.entity.PageBean;
import cn.blog.service.BlogTypeService;
import cn.blog.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("blogType")
public class BlogTypeController {

    @Resource
    private BlogTypeService blogTypeService;

    /**
     * 查询控制器
     * @param page
     * @param rows
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping({"/list"})
    public String list(@RequestParam(value = "page", required = false) String page,
                       @RequestParam(value = "rows", required = false) String rows,
                       HttpServletResponse response) throws Exception {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<Blogtype> blogtypeList = blogTypeService.list(map);
        //查询总共有多少条数据
        Long total = blogTypeService.getTotal(map);
        JSONObject result=new JSONObject();
        JSONArray jsonArray=JSONArray.fromObject(blogtypeList);
        result.put("rows",jsonArray);
        result.put("total",total);
        ResponseUtil.write(response,result);
        return null;
    }

    @RequestMapping({"/save"})
    public String save(Blogtype blogtype,HttpServletResponse response) throws Exception {
        int resultTotal=0;
        if (blogtype.getId()==null) {
            //id为空时，说明是添加操作
            resultTotal = blogTypeService.add(blogtype).intValue();
        }else {
            //id为空时，说明是修改操作
            resultTotal = blogTypeService.update(blogtype).intValue();
        }
        JSONObject result=new JSONObject();
        if (resultTotal>0){
            result.put("success",Boolean.valueOf(true));
        }else{
            result.put("success",Boolean.valueOf(false));
        }
        ResponseUtil.write(response,result);
        return null;
    }

    /**
     * 删除
     * @param ids
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping({"/delete"})
    public String delete(@RequestParam("ids")String ids,
                         HttpServletResponse response) throws Exception {
        String[] idsStr=ids.split(",");
        JSONObject result=new JSONObject();
        for (int i=0;i<idsStr.length;i++){
            blogTypeService.delete(Integer.valueOf(idsStr[i]));
        }
        result.put("success",Boolean.valueOf(true));
        ResponseUtil.write(response,result);
        return null;
    }

}
